package com.example.template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.FileUtils;

import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.example.freemark.ITemplateEngine;
import com.example.util.ExceptionUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FreeMarkerEngine implements ITemplateEngine {

	private TemplateConfig templateConfig;
	private Configuration configuration;
	
	public FreeMarkerEngine(TemplateConfig templateConfig) {
		this.templateConfig = templateConfig;
		clearOldFiles();
		initFreeMarker();
	}

	@Override
	public void execute(String templateFileType) {
		try {
			Template template = configuration.getTemplate("temp/"+getTemplateFile(templateFileType)+".ftl");
			Map<String,String> map = new HashMap<>();
			String outputFile = getOutPutFile(templateFileType,map);
			FileOutputStream fileOutputStream = FileUtils.openOutputStream(new File(outputFile));
			template.process(templateConfig, new OutputStreamWriter(fileOutputStream, Constant.UTF8));
			log.debug("模板:" + templateFileType + ";  文件:" + outputFile);
		} catch (Exception e) {
			throw ExceptionUtils.createRuntimeException(e);
		}
	}

	private String getTemplateFile(String templateFileType) {
		try {
			InputStream popInput = TemplateConfig.class.getClassLoader()
					.getResourceAsStream("templateMapping.properties");
			Properties pop = new Properties();
			pop.load(popInput);
			return pop.getProperty(templateFileType);
		} catch (Exception e) {
			throw ExceptionUtils.createRuntimeException(e);
		}
	}
	
	private String getOutPutFile(String templateFileType, Map<String, String> map) {
		
		String packagePath = templateConfig.getProjectPackage().replaceAll("\\.", "/");
		String fileName = templateConfig.getModelName()+getTemplateFile(templateFileType)+".java";
		String mvcName = "common";
		String modelPath = "";
		switch (templateFileType) {
		//service
		case "service.add":
		case "service.del":
		case "service.infoQuery":
		case "service.listQuery":
			mvcName = "service";
			modelPath = "biz/service/impl";
			break;
		case "service.config":
			mvcName = "service";
			return templateConfig.getOutPath()+mvcName+"/config/service-"+templateConfig.getModelName().toLowerCase()+".xml";
		//枚举
		case "common.enum":
			return templateConfig.getOutPath()+mvcName+"/enum.java";
		//控制器
		case "controller.all":
			mvcName = "controller";
			modelPath = "mvc/controller";
			break;
		case "model.add":
		case "model.info":
		case "model.list":
			mvcName = "model";
			modelPath = "model";
			break;
		//MODEL
		default:
			break;
		}
		return templateConfig.getOutPath()+mvcName+"/"+packagePath+"/"+modelPath+"/"+fileName;
	}

	public void initFreeMarker() {
		configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        configuration.setDefaultEncoding(ConstVal.UTF8);
        configuration.setClassForTemplateLoading(TemplateConfig.class, Constant.SLASH);
	}
	
	private void clearOldFiles() {
		try {
			FileUtils.deleteDirectory(new File(templateConfig.getOutPath()));
		} catch (IOException e) {
			throw ExceptionUtils.createRuntimeException(e);
		}
	}

}
